package utils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

/**
 * Created by Liheng on 2017-04-16.
 */

public class DataUtils {

	/**
	 * 获取验证码
	 *
	 * @param cookie
	 * @return
	 */
	public static byte[] getVerCode(String urlReferer, String cookie) {

		Network network;
		byte[] imageByte = null;
		try {
			network = new Network(new URL(Constents.URL_VALIDATE_CODE), Network.METHOD_GET, urlReferer, cookie);
			imageByte = network.getBytes();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return imageByte;
	}

	/**
	 * 从网上获取课表信息
	 *
	 * @param settingParams
	 *            设置参数
	 * @param dataParams
	 *            数据参数
	 * @return
	 */
	public static String searchFromNetworkCourse(Map<String, String> settingParams, Map<String, String> dataParams) {

		String html = "";
		try {
			String cookie = settingParams.get("cookie");
			String urlCourse = settingParams.get("urlCourse");
			String urlReferer = settingParams.get("urlReferer");
			Network network = new Network(new URL(urlCourse), Network.METHOD_POST, urlReferer, cookie);
			html = network.getData(dataParams, Network.EDIT_CODE_GBK);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return html;
	}

	/**
	 * 获取cookie
	 * 
	 * @param urlAddress
	 * @return
	 */
	public static String getCookie(String urlAddress) {
		String cookie = "";
		try {
			Network network = new Network(new URL(urlAddress), Network.METHOD_GET);
			if (network.connectNetwork()) {
				cookie = network.getCookie();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cookie;
	}

	/**
	 * 获取下拉列表信息
	 * 
	 * @param urlAddress
	 * @return
	 */
	public static String getOptionInfoFromNetWork(String urlAddress) {
		String html = "";
		Network network;
		try {
			network = new Network(new URL(urlAddress), Network.METHOD_GET);
			if (network.connectNetwork()) {
				html = network.getData(Network.EDIT_CODE_GBK);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return html;

	}

}
